/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;

namespace ArtNotes
{


	public class ANImageView: UIView
	{
		//private UIScrollView imageScrollView;
		//private UIImageView imageScrollViewImage;
		private UIButton polaroidButton;
		
		private UIImageView polaroidFrame;
		private UIImage polaroidImage;
		private Image image;
		
		private UIImage scaledPolaroidImage;
		private UIImageView polaroidBackground;

		public ANImageView (Image img)
		{
			if(img == null)
				Console.WriteLine("img is null");
			
			this.image = img;
			
			polaroidFrame = new UIImageView();
			
			this.Frame = new RectangleF (0, 0, 320, 370);
			this.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			
			polaroidFrame.Image = UIImage.FromFile("Images/large_polaroid.png");
			polaroidFrame.Frame = new RectangleF(30,10,266,283);
			
			polaroidImage = UIImage.FromFileUncached(AN_APP_OPTIONS.DOC_DIR + image.FileName);
			
			ANImageUtils anImageUtils = new ANImageUtils();
			scaledPolaroidImage = anImageUtils.scaleImage(polaroidImage,200, 220);
			
			polaroidBackground = new UIImageView(new RectangleF(60,30,200,220));
			polaroidBackground.Image = UIImage.FromFile("Images/polaroidBackground.png");
			
			PointF pf = new PointF();
			
			pf.X = 60 + ((200 - scaledPolaroidImage.Size.Width) / 2);
			pf.Y = 30 + ((220 - scaledPolaroidImage.Size.Height) / 2);
			
			polaroidButton = ImageButtonType(
			                                 new RectangleF(pf,scaledPolaroidImage.Size),
			                                 scaledPolaroidImage,
			                                 scaledPolaroidImage
			                                 );
			
			polaroidButton.TouchDown += delegate {
				//UIImage fsi = downloadImage(photo.SmallUrl,photo.PhotoId + ".jpg");
				//ANFullScreenImage anFullScreenImage = new ANFullScreenImage(polaroidImage);
				//this.NavigationController.PresentModalViewController(anFullScreenImage,true);
				Console.WriteLine("fullscreen");
			};
			
			this.AddSubview(polaroidFrame);
			this.AddSubview(polaroidBackground);
			this.AddSubview(polaroidButton);
			
			//ANImageUtils anImageUtils = new ANImageUtils();
			
			//214x226
			//kasabian - west rider pauper lunatic asylum
			
			//polaroidImage = anImageUtils.overlayImageWithPolaroid(
			//                                      				UIImage.FromFileUncached(AN_APP_OPTIONS.DOC_DIR + img.FileName),
			//                                      				polaroidFrame,
			//                                                     new RectangleF(0,0,214,226),
			//                                                     new RectangleF(0,0,214,226),
			//                                                     new SizeF(214,226)
			//                                                     );
			
			//polaroidImage.Image = UIImage.FromFile("Images/large_polaroid.png");
			//polaroidImage.Frame = new RectangleF(0,0,320,340);
			//this.AddSubview(polaroidImage);
			
			//UILabel uil = new UILabel(new RectangleF(10f,10f,100f,25f));
			//uil.Text = "ANImageView";
			//this.AddSubview(uil);
			
			
			//imageScrollViewImage = new UIImageView(UIImage.FromFileUncached(AN_APP_OPTIONS.DOC_DIR + img.FileName));
			//imageScrollViewImage.Frame = new RectangleF (0,0,270,250);
			
			//imageScrollView = new UIScrollView();
			
			//imageScrollView.ContentSize = new SizeF(300f,260f);

			//imageScrollView.MaximumZoomScale = 6.0f;
			//imageScrollView.MinimumZoomScale = 1.5f;
			//imageScrollView.ClipsToBounds = true;
			//imageScrollView.Frame = new RectangleF (20,20,270,270);
			//imageScrollView.BackgroundColor = UIColor.White;
			//imageScrollView.AddSubview(imageScrollViewImage);
			
			//imageScrollView.Delegate = new scrollViewDelegate(this);
		}
		
		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			return button;
		}
		
		/*public class scrollViewDelegate : UIScrollViewDelegate
		{
			private ANImageView anImageView;
			
			public scrollViewDelegate(ANImageView anImageView)
			{
				this.anImageView = anImageView;
			}
			
			public override UIView ViewForZoomingInScrollView (UIScrollView scrollView)
			{
				return this.anImageView.imageScrollViewImage;
			}

		}*/
	}
}
